package org.sxp.modules.workspace.controller;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.validation.annotation.Validated;
import org.sxp.modules.sys.entity.SysDeptEntity;
import org.sxp.modules.sys.entity.SysUserEntity;
import org.sxp.modules.sys.service.SysDeptService;
import org.sxp.modules.workspace.entity.SpaceDO;
import org.sxp.modules.workspace.service.SpaceService;
import org.sxp.modules.workspace.dto.SpaceDTO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.sxp.common.annotation.SysLog;
import org.sxp.common.utils.PageInfo;
import org.sxp.common.utils.PageResult;
import org.sxp.common.utils.Query;
import org.sxp.common.web.WrapperResponse;
import org.sxp.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;

/**
 * 
 * @author 沈兴平
 * @date 2021/12/29
 */
@RestController
@RequestMapping(value = "/workspace/space")
@Api(description="Space", tags = "")
@SysLog(modules = "workspace", value = "")
public class SpaceController extends AbstractController{
    @Autowired
    private SpaceService spaceService;

    @Autowired
    private SysDeptService sysDeptService;

    /**
     * 列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageNum", value = "当前页码", dataType="int", paramType = "header"),
            @ApiImplicitParam(name="pageSize",value="每页条目",dataType="int", paramType = "header"),
            @ApiImplicitParam(name="orderField",value="排序字段",dataType="string", paramType = "header"),
            @ApiImplicitParam(name="orderType",value="排序类型",dataType="string", paramType = "header", example = "asc或者desc")
    })
    @RequestMapping(value = "/list",method = {RequestMethod.POST})
    @ApiOperation(value = "查询列表")
    @RequiresPermissions("workspace:space:list")
    public WrapperResponse<List<SpaceDTO>> list(SpaceDTO params) throws Exception{
        //请求头获取分页参数
        PageInfo pageInfo = buildPageInfo();
        IPage<SpaceDO> page = spaceService.page(
                new Query<SpaceDO>().getPage(pageInfo)
                ,new LambdaQueryWrapper<SpaceDO>());

        for(SpaceDO entity : page.getRecords()){
            SysDeptEntity sysDeptEntity = sysDeptService.getById(entity.getDeptId());
            if(sysDeptEntity != null){
                entity.setDeptName(sysDeptEntity.getName());
            }
        }
        return (WrapperResponse)WrapperResponse.success(new PageResult(page));
    }


    /**
     * 信息
     */
    @RequestMapping(value = "/info/{id}", method = {RequestMethod.GET})
    @ApiOperation(value = "查询")
    @RequiresPermissions("workspace:space:info")
    public WrapperResponse<SpaceDTO> info(@PathVariable Long id) throws Exception{
        SpaceDO space = spaceService.getById(id);

        return (WrapperResponse)WrapperResponse.success(space);
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save",method = {RequestMethod.POST})
    @ApiOperation(value = "保存")
    @SysLog("保存")
    @RequiresPermissions("workspace:space:save")
    public WrapperResponse<SpaceDTO> save(@RequestBody SpaceDTO space) throws Exception{
        spaceService.save(SpaceDO.copyBean(space));

        return (WrapperResponse)WrapperResponse.success(null);
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update",method = {RequestMethod.POST})
    @ApiOperation(value = "修改")
    @SysLog("修改")
    @RequiresPermissions("workspace:space:update")
    public WrapperResponse<SpaceDTO> update(@RequestBody SpaceDTO space) throws Exception{
        spaceService.updateById(SpaceDO.copyBean(space));
        
        return (WrapperResponse)WrapperResponse.success(null);
    }

    /**
     * 批量删除
     */
    @RequestMapping(value = "/deleteBatch",method = {RequestMethod.POST})
    @ApiOperation(value = "批量删除")
    @SysLog("批量删除")
    @RequiresPermissions("workspace:space:deleteBatch")
    public WrapperResponse deleteBatch(@RequestBody Long[] ids) throws Exception{
        spaceService.removeByIds(Arrays.asList(ids));

        return WrapperResponse.success(null);
    }
}
